#!/usr/bin/env python

#        +-----------------------------------------------------------------------------+
#        | GPL                                                                         |
#        +-----------------------------------------------------------------------------+
#        | Copyright (c) Mattia Lambertini <lamberti@cs.unibo.it>                      |
#        |                                                                             |
#        | This program is free software; you can redistribute it and/or               |
#        | modify it under the terms of the GNU General Public License                 |
#        | as published by the Free Software Foundation; either version 2              |
#        | of the License, or (at your option) any later version.                      |
#        |                                                                             |
#        | This program is distributed in the hope that it will be useful,             |
#        | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
#        | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
#        | GNU General Public License for more details.                                |
#        |                                                                             |
#        | You should have received a copy of the GNU General Public License           |
#        | along with this program; if not, write to the Free Software                 |
#        | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
#        +-----------------------------------------------------------------------------+

try:
    import pygtk
    #tell pyGTK, if possible, that we want GTKv2
    pygtk.require("2.0")
except:
    #Some distributions come with GTK2, but not pyGTK
    pass

try:
    import gtk
    import gtk.glade
except:
    print "You need to install pyGTK or GTKv2 ",
    print "or set your PYTHONPATH correctly."
    print "try: export PYTHONPATH=",
    print "/usr/local/lib/python2.2/site-packages/"
    sys.exit(1)

import postit_list
from __init__ import VARIABLES

__author__="Mattia Lambertini"
__date__ ="$Aug 22, 2009 8:46:09 PM$"

class Postit(object):

    windowsStatus = 0

    def __init__(self, _date, father, content= ""):

        windowname="noteWindow"
        self._id = _date
        self.father = father
        self.wTree=gtk.glade.XML (VARIABLES['GLADE_FILE'], windowname)
        self.window = self.wTree.get_widget(windowname)
        pixmap, mask = gtk.gdk.pixmap_create_from_xpm(self.window.window, None, VARIABLES['POSTIT_IMG'])
        image = self.wTree.get_widget("noteImage")
        image.set_from_pixmap(pixmap, mask)
        image.show()

        # configure the post-it window
#        self.window.set_events(self.window.get_events() | gtk.gdk.BUTTON_PRESS_MASK
#                                                        | gtk.gdk.BUTTON_RELEASE_MASK)
#        self.window.connect("button_press_event", self.click_note)
#        self.window.connect("button_release_event", self.move_note)
#        self.window.connect("delete_event", self.delete_event)

        # load the close button images
        self.vCloseButtonPixBuf = gtk.gdk.pixbuf_new_from_file(VARIABLES['CLOSE_IMG'])
        self.hCloseButtonPixBuf = gtk.gdk.pixbuf_new_from_file(VARIABLES['HIDE_CLOSE_IMG'])
        # setup the delete postit button
        self.closeImage = self.wTree.get_widget("closeImage")
        self.closeImage.set_from_pixbuf(self.hCloseButtonPixBuf)
        self.event_box = self.wTree.get_widget("closeEventBox")
        self.event_box.connect("button_press_event", self.delete_note)
        self.event_box.connect("enter_notify_event", self.show_close_button)
        self.event_box.connect("leave_notify_event", self.hide_close_button)
        
        self.window.stick()
        self.window.set_keep_below(True)
        # init the text editor
        self.__init_text_view(content.strip())
        
        # This masks out everything except for the image itself
        self.window.shape_combine_mask(mask, 0, 0)

        # self.create_postit()

    def __init_text_view(self, content=""):
        """ """
        noteTextView=self.wTree.get_widget("noteTextView")
        self.noteTextBuffer=gtk.TextBuffer(None)
        self.noteTextBuffer.set_text(content)
        self.noteTextBuffer.set_modified(False)
        noteTextView.set_buffer(self.noteTextBuffer)
        style = noteTextView.get_style().copy()
        map = noteTextView.get_colormap()
        colour = map.alloc_color("#e1f915") # the color of the post-it
        style.base[gtk.STATE_NORMAL] = colour
        noteTextView.set_style(style)
        noteTextView.connect('focus-out-event', self.save_textview)

    def save_textview(self, widget, event, data=None):
        """ Save the buffer of the textView into the db if the buffer has been modified
        till the last time this method has been called"""
        
        if self.noteTextBuffer.get_modified() == True:
            start,end = self.noteTextBuffer.get_bounds()
            postit_list.update_postit(self._id,
                                        self.noteTextBuffer.get_text(start, end),
                                        VARIABLES['DB_NAME'])
            self.noteTextBuffer.set_modified(False)

    def delete_note(self, widget, event, data=None):
        """ """
        self.window.destroy()
        
        postit_list.delete_postit(self._id)
        if self in self.father.postit_list:
            self.father.postit_list.remove(self)


    def show_close_button(self, widget, event, data=None):
        """ """
        self.closeImage.set_from_pixbuf(self.vCloseButtonPixBuf)
        

    def hide_close_button(self, widget, event, data=None):
        """ """
        self.closeImage.set_from_pixbuf(self.hCloseButtonPixBuf)

#    def move_note(self, widget, event, data=None):
#        """ move the note  from the origin coordinates (top left point)
#            by a vector equal of the mouse moves
#        """
#
#        self.x = self.x + (event.x - self.click_x)
#        self.y = self.y + (event.y - self.click_y)
#        self.window.move(int(self.x), int(self.y))

#    def click_note(self, widget, event, data=None):
#        """ check and save the coordinates of mouse click
#            and of the actual origin
#        """
#
#        self.x, self.y = self.window.get_position()
#        self.click_x = event.x
#        self.click_y = event.y

    def set_status(self, visible):
        """ show and hide all the notes on the desktop
        """
      # self.window.get_property("visible")
        if visible == True and self.window.get_property("visible") == False:
            self.show_postit()
        elif visible == False and  self.window.get_property("visible") == True:
            self.hide_postit()
           


    def show_postit(self):
        """ show the postit associated to self
            on the desktop
        """
        
        self.window.present()
        self.window.stick()
        self.window.set_keep_below(True)

    def hide_postit(self):
        """ hide the postit associated to self
            on the desktop
        """
        
        self.window.hide()
        
    def create_postit(self):
        """"""
        self.window.show()

#    def delete_event(self, widget, event, data=None):
#        """"""
#        gtk.main_quit()
#        return False


if __name__ == "__main__":
    print "postit.py DEBUG MODE";
